﻿// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.BabelCam.Infrastructure.Helpers
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Net;
    using System.ServiceModel.Web;
    using Microsoft.Samples.BabelCam.Infrastructure.Models;
    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;

    [CLSCompliant(false)]
    public class TranslatedWordsServiceContext : TableServiceContext, ITranslatedWordsRepository
    {
        public const string TranslatedWordsTableName = "BabelCamTranslatedWords";

        private CloudStorageAccount account;
        private IUserPrivilegesRepository userRepository;

        public TranslatedWordsServiceContext()
            : this(CloudStorageAccount.FromConfigurationSetting("DataConnectionString"), new UserTablesServiceContext())
        {
            // Create table if it does not exists and set it to public
            this.CreateTable();
        }

        public TranslatedWordsServiceContext(CloudStorageAccount account, IUserPrivilegesRepository userRepository)
            : this(account.TableEndpoint.ToString(), account.Credentials, userRepository)
        {
            this.account = account;
        }

        public TranslatedWordsServiceContext(string baseAddress, StorageCredentials credentials, IUserPrivilegesRepository userRepository)
            : base(baseAddress, credentials)
        {
            this.userRepository = userRepository;
            this.IgnoreResourceNotFoundException = true;
            this.IgnoreMissingProperties = true;
        }

        public void AddTranslatedWords(WordsEntity words)
        {
            this.CreateTable();
            this.AddObject(TranslatedWordsTableName, words);
            this.SaveChanges();
        }

        private void CreateTable()
        {
            // Since the table is public, we must create it every time 
            var cloudTableClient = this.account.CreateCloudTableClient();
            cloudTableClient.CreateTableIfNotExist(TranslatedWordsServiceContext.TranslatedWordsTableName);

            var privilege = string.Format(CultureInfo.InvariantCulture, "{0}{1}", TranslatedWordsServiceContext.TranslatedWordsTableName, PrivilegeConstants.PublicTablePrivilegeSuffix);
            if (!this.userRepository.PublicPrivilegeExists(privilege))
            {
                this.userRepository.AddPublicPrivilege(privilege);
            }
        }
    }
}